package com.duan.goods.controller;

import com.duan.entity.PageResult;
import com.duan.entity.Result;
import com.duan.entity.StatusCode;
import com.duan.goods.pojo.Product;
import com.duan.goods.service.ProductService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品表
 */
@RestController
@RequestMapping("/Product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 查询全部
     * @param
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<Product>> selectAll(){
        return Result.ok("查询成功",productService.selectAll());
    }


    /**
     * 条件查询
     * @param product
     * @return
     */
    @PostMapping("/selectList")
    public Result< List<Product> > select(@RequestBody Product product){
        return Result.ok("条件查询成功",productService.selectList(product));
    }

    /**
     * 新增商品
     * @param product
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Product product){
        productService.insert(product);
        return Result.ok("新增成功");
    }


    /**
     * 删除商品
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        productService.delete(ids);
        return Result.ok("删除成功");
    }


    /**
     * 修改商品
     * @param product
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Product product){
        productService.update(product);
        return Result.ok("修改成功");
    }

    /**
     * 分页查询
     * @param page 默认值1
     * @param size 默认值10
     * @return
     */
    @GetMapping(value = "/pageAll" )
    public Result pageAll(@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Product> pageList = productService.pageAll(page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }

    /**
     * 分页条件查询
     * @param product
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/pageList" )
    public Result pageList(@RequestBody Product product,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Product> pageList = productService.pageList(page,size,product);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }
}
